/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesis2.pucp.Envio.DAO;

import com.tesis2.pucp.Modelo.TesisCliente;
import com.tesis2.pucp.Util.HibernateUtil;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author pachon
 */
public class ClienteDAO {
    
    public void guardarCliente(TesisCliente cliente){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx =  session.beginTransaction();
        try {
            session.saveOrUpdate(cliente);
            tx.commit();
        }catch(HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();            
        }        
    }
    
    public ArrayList<TesisCliente> listarClientes()
    {
        ArrayList<TesisCliente> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from TesisCliente order by idcliente").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                TesisCliente p = (TesisCliente) iter.next();
                Hibernate.initialize(p.getApellidomaterno());
                Hibernate.initialize(p.getApellidopaterno());
                Hibernate.initialize(p.getDireccion());
                Hibernate.initialize(p.getDni());
                Hibernate.initialize(p.getEstado());
                Hibernate.initialize(p.getEmail());
                Hibernate.initialize(p.getNombre());
                Hibernate.initialize(p.getTelefono());
                Hibernate.initialize(p.getIdcliente());
                listaResultados.add(p);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados;
        }         
    }    
    
}
